package cetcbigdata.da.personal_service.xinjiang;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.base.DownloadAndUploadMinio;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class XJpersonUtil extends Base {

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
    }

    @Override
    public JSONObject getDataDemo() {
        return null;
    }

    //基本信息
    public  static JSONObject getBasicInfo(JSONObject custom) {
        JSONObject basic = custom.getJSONObject("basic");
        JSONObject result = new JSONObject();
        result.put("事项类型", basic.getString("tasktype"));
        result.put("办件类型", basic.getString("projecttype"));
        result.put("事项编码", basic.getString("taskhandleitem"));
        result.put("到办事现场次数", isNotBlankOrNull(basic.getString("limitscenenum"))?
                basic.getString("limitscenenum")+"次" : "无");
        result.put("网上办理深度", basic.getString("onlinehandledepth"));
        result.put("实施主体", basic.getString("deptname"));
        result.put("行使层级", basic.getString("uselevel"));
        result.put("承诺办结时限", basic.getString("promiseday") + basic.getString("promisetype"));
        result.put("法定办结时限", basic.getString("anticipateday") + basic.getString("anticipatetype"));
        result.put("咨询方式", basic.getString("linkway"));
        result.put("监督投诉方式", basic.getString("superviseway"));
        result.put("办理时间", basic.getString("transacttime"));
        result.put("办理地点", basic.getString("transactaddr"));
        result.put("办理形式", basic.getString("handletype"));
        result.put("是否进驻政务大厅", custom.getJSONObject("status").getString("isentrycenter").equals("1")? "是" : "否");
        result.put("实施主体性质", basic.getString("depttype"));
        result.put("权力来源", basic.getString("powersource"));
        result.put("通办范围", basic.getString("handlearea"));
        result.put("联办机构", basic.getString("otherdept"));
        result.put("服务对象", basic.getString("servertype"));
        return result;
    }

    //办理材料
    public static JSONArray getMaterials(JSONArray materialscase) {
        try {
            JSONArray result = new JSONArray();
            for (Object o : materialscase){
                JSONObject obj = (JSONObject) o;
                for (Object o1 : obj.getJSONArray("materials")){
                    JSONObject data = (JSONObject) o1;
                    JSONObject material = new JSONObject();
                    material.put("材料名称", data.getString("materialname"));
                    material.put("材料类型", data.getString("papermaterialtype"));
                    material.put("材料形式", data.getString("materialformat"));
                    material.put("材料必要性", data.getString("isneed"));
                    material.put("来源渠道", data.getString("sourcetype"));
                    material.put("来源渠道说明", data.getString("sourceexplain"));
                    material.put("纸质原件材料份数", data.getString("masternum"));
                    material.put("纸质复印件材料份数", data.getString("copynum"));

                    //下载封装空白表和详表
                    String id = data.getString("formguid");
                    if (null != id && id.length()>0) {
                        try {
                            JSONObject emptyFile = new JSONObject();
                            String emptyFileUrl = "https://zwfw.xinjiang.gov.cn/xjzwdt/rest/attach/openAttach?client=" + id;
                            String emptyFileMinioId = fileDownloadAndUploadMinioGetImage(emptyFileUrl, id);
                            emptyFile.put("fileUrl", emptyFileUrl);
                            emptyFile.put("minioId", emptyFileMinioId);
                            material.put("空白表格", emptyFile);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    } else {
                        material.put("空白表格", "无");
                    }
                    id = data.getString("exampleguid");
                    if (null != id && id.length()>0) {
                        try {
                            JSONObject sampleFile = new JSONObject();
                            String sampleFileUrl = "https://zwfw.xinjiang.gov.cn/xjzwdt/rest/attach/openAttach?client=" + id;
                            String sampleFileMinioId = fileDownloadAndUploadMinioGetImage(sampleFileUrl, id);
                            sampleFile.put("fileUrl", sampleFileUrl);
                            sampleFile.put("minioId", sampleFileMinioId);
                            material.put("示例表格", sampleFile);
                        }catch (Exception e){
                            material.put("示例表格", "无");
                        }


                    } else {
                        material.put("示例表格", "无");
                    }

                    material.put("受理标准", data.getString("acceptstand"));
                    material.put("纸质材料规格", data.getString("pageformat"));
                    material.put("填报须知", data.getString("fillexplain"));
                    material.put("要求提供材料的依据", data.getString("bylaw"));

                    result.add(material);
                }
            }
            return result;
        }catch (Exception e){
            return null;
        }
    }

    //办理流程
    public static JSONObject getFlowLCs(JSONObject custom) {
        JSONObject result = new JSONObject();
        JSONObject extend = custom.getJSONObject("extend");
        //网上办理流程和窗口办理流程
        String id = extend.getString("onlineflowguid");
        try {
            JSONObject online = new JSONObject();
            JSONObject file = new JSONObject();
            String fileUrl = "https://zwfw.xinjiang.gov.cn/xjzwdt/rest/attach/openAttach?client=" + id;
            String fileMinioId = fileDownloadAndUploadMinioGetImage(fileUrl, id);
            file.put("url", fileUrl);
            file.put("minioId", fileMinioId);
            online.put("网上办理流程", extend.getString("onlineflowexplain"));
            online.put("流程图", file);
            result.put("网上办理", online);
        } catch (Exception e) {
            result.put("网上办理", "无");
        }
        id = extend.getString("handleflowguid");
        try {
            JSONObject handle = new JSONObject();
            JSONObject file = new JSONObject();
            String fileUrl = "https://zwfw.xinjiang.gov.cn/xjzwdt/rest/attach/openAttach?client=" + id;
            String fileMinioId = fileDownloadAndUploadMinioGetImage(fileUrl, id);
            file.put("url", fileUrl);
            file.put("minioId", fileMinioId);
            handle.put("窗口办理流程", extend.getString("handleflow"));
            handle.put("流程图", file);
            result.put("窗口办理", handle);
        }catch (Exception e){
            result.put("窗口办理", "无");
        }

        //办理步骤
        try {
            JSONArray processs =  custom.getJSONArray("processs");
            JSONArray buzhouList = new JSONArray();
            for (Object o : processs){
                JSONObject process = (JSONObject) o;
                JSONObject buzhouValue = new JSONObject();
                buzhouValue.put("步骤名称", process.getString("process_name"));
                buzhouValue.put("办理结果", process.getString("result"));
                buzhouValue.put("审批标准", process.getString("approval_standard"));
                buzhouValue.put("办理时间", process.getString("process_time") + "个工作日");
                buzhouValue.put("办理人员", process.getString("user_name"));
                buzhouList.add(buzhouValue);
            }
            result.put("步骤", buzhouList);
        } catch (Exception e) {
            result.put("步骤", "无");
        }
        return result;
    }

    //收费标准
    public static Object getFeeProject(JSONArray objects) {
        JSONArray result = new JSONArray();

        if (null != objects && objects.size() > 0) {
            for (Object o : objects){
                JSONObject object = (JSONObject) o;
                JSONObject feeproject = new JSONObject();
                feeproject.put("收费项目名称", object.getString("feename"));
                feeproject.put("是否允许减免", isNotBlankOrNull(object.getString("isdesc"))?
                        object.getString("isdesc") : "否");
                feeproject.put("收费依据", object.getString("bylaw"));
                feeproject.put("收费标准", object.getString("feestand"));
                feeproject.put("备注", object.getString("remark"));
                feeproject.put("是否支持网上收费", "不支持");
                result.add(feeproject);
            }
            return result;
        } else {
            return "无收费内容";
        }
    }

    //设定依据
    public static Object getbylaw(JSONArray objects) {
        JSONArray results = new JSONArray();
        if (null != objects && objects.size() > 0){
            for (Object o : objects){
                JSONObject object = (JSONObject) o;
                JSONObject bylaw = new JSONObject();
                bylaw.put("法律法规名称", object.getString("lawname"));
                bylaw.put("依据文号", object.getString("accordingnumber"));
                bylaw.put("条款号", object.getString("termsnumber"));
                bylaw.put("条款内容", object.getString("termscontent"));
                results.add(bylaw);
            }
            return results;
        }else {
            return "无设定依据";
        }
    }

    //常见问题
    public static Object getquestion(JSONArray objects) {
        JSONArray results = new JSONArray();
        if (null != objects && objects.size() > 0){
            for (Object o : objects){
                JSONObject object = (JSONObject) o;
                JSONObject question = new JSONObject();
                question.put("问题", object.getString("question"));
                question.put("回答", object.getString("answer"));
                results.add(question);
            }
            return results;
        }else {
            return "无常见问题";
        }
    }

    public static String fileDownloadAndUploadMinioGetImage(String url, String fileName ) throws Exception {
        JSONObject param = new JSONObject();
        JSONObject headers = new JSONObject();
        headers.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");

        //=========================================================测试性注释以下代码===============================================================
        return DownloadAndUploadMinio.getInstance().downloadGet(
                url,
                headers,
                param,
                fileName
        );
//        return null;
    }
}
